package com.gitee.feizns.itrip.personal.search.entity.vo;

import com.gitee.feizns.itrip.personal.search.entity.Facilities;
import com.gitee.feizns.itrip.personal.utils.Utils;
import lombok.Getter;
import lombok.ToString;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author feizns
 * @since 2019/1/5 0005
 */
@ToString
public class HotelData {

    /**
     * 继承属性数量上限
     */
    private static final int EXTEND_PROPERTY_LIMIT = 5;

    /**
     * 地址
     */
    @Getter
    private String address;

    /**
     * 平均评价
     */
    @Getter
    private String avgScore;

    /**
     * 评论数
     */
    @Getter
    private Integer commentCount;

    /**
     * 酒店设备
     */
    private String facilities;

    /**
     * 酒店特色
     */
    @Getter
    private String featureNames;

    /**
     * 酒店等级
     */
    @Getter
    private Integer hotelLevel;

    /**
     * 酒店名称
     */
    @Getter
    private String hotelName;

    /**
     * 酒店编号
     */
    @Getter
    private Long id;

    /**
     * 酒店图片
     */
    @Getter
    private String imgUrl;

    /**
     * 是否满意个数
     */
    @Getter
    private Integer isOkCount;

    /**
     * 最大金额
     */
    @Getter
    private Long maxPrice;

    /**
     * 最小金额
     */
    @Getter
    private Long minPrice;

    /**
     * 城市名称
     */
    @Getter
    private String redundantCityName;

    /**
     * 国家名称
     */
    @Getter
    private String redundantCountryName;

    /**
     * 省份名称
     */
    @Getter
    private String redundantProvinceName;

    /**
     * 商业位置
     */
    @Getter
    private String tradingAreaNames;

    private List<Facilities> extendProperty;


    public String getExtendsPropertyNames() {
        return mapTo(Facilities.Leaf::getName);
    }

    public String getExtendPropertyPics() {
        return mapTo(Facilities.Leaf::getPic);
    }

    private <T> String mapTo(Function<Facilities.Leaf, String> getter) {
        if ( extendProperty == null )
            extendProperty = Utils.parseArray(facilities, Facilities.class);

        return extendProperty.stream()
                .flatMap(item -> item.getLeafs() != null ? item.getLeafs().stream() : null).map(getter).limit(EXTEND_PROPERTY_LIMIT)
                .collect(Collectors.joining(","));
    }

}
